home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / DDEIO.ZIP;1 / DDEIO.PRG < prev    next >
Encoding:
Text File  |  1993-11-21  |  9.0 KB  |  415 lines

  1. /*
  2. **      DDEIO.PRG
  3. **
  4. **      This module contains Dynamic Data Exchage support for clipper
  5. **      programs using the DDEDRV device driver.
  6. **
  7. **      (c) 1993 Copyright Portable Software, Inc.
  8. **      All Rights Reserved.
  9. **
  10. **      This source code is provided "as is", with no warranties expressed
  11. **      or implied.
  12. */
  13.  
  14. #include "fileio.ch"
  15.  
  16. /* error codes */
  17.  
  18. #define         ERR_AGENT_ACK           0
  19. #define         ERR_ACK_ID              1
  20. #define         FIRST_ERROR             10
  21. #define         ERR_AGENT_NACK          10
  22. #define         ERR_IN_USE              11
  23. #define         ERR_TIMEOUT             12
  24. #define         ERR_INVALID_COMMAND     13
  25. #define         ERR_PARM_TYPE           14
  26. #define         ERR_PARM_ERROR          15
  27. #define         ERR_DRIVER              16
  28. #define         ERR_COMMAND_LINE        17
  29. #define         ERR_UNKNOWN_PARM        18        
  30. #define         ERR_INTERNAL            23
  31.  
  32. /* misc defines */
  33.  
  34. #define         DDE_DEFAULT_WAIT        20
  35.  
  36.  
  37. /****************************DDEInitiate**********************************
  38. **
  39. ** Initiate a conversation with a Windows program.
  40. **
  41. ** Input :
  42. ** Application name and topic name to communicate with.
  43. ** The waittime value represents seconds and is used to timeout 
  44. ** the request.
  45. **
  46. ** Output :
  47. ** The value returned is a number between 1 or 8 if the call 
  48. ** is successful.  A number between 1 and 8 indicates an open channel
  49. ** through which all DDE verbs can be used.  Before any other verbs
  50. ** can be used, i.e. DDEPoke, DDERequest, DDEExecute or DDETerminate
  51. ** a successful DDEInitiate call must be performed.
  52. ** 
  53. ** Example:    channel = DDEInitiate("PROGMAN", "PROGMAN", 30)
  54. **
  55. ** This example opens up a channel with the Windows program 
  56. ** manager.
  57. ************************************************************************/
  58.  
  59.  
  60. FUNCTION DDEInitiate(app, topic, waittime)
  61. LOCAL nHandle, timeout, rc
  62.  
  63. IF app = NIL .OR. topic = NIL 
  64.    RETURN ERR_PARM_ERROR 
  65. ENDIF
  66.  
  67. IF VALTYPE(app) != "C" .OR. VALTYPE(topic) != "C"
  68.    RETURN ERR_PARM_TYPE
  69. ENDIF
  70.  
  71. IF waittime = NIL
  72.    waittime = DDE_DEFAULT_WAIT
  73. ENDIF
  74.  
  75. /* open the dde device */
  76.  
  77. nHandle = FOPEN("ddedrv", FO_READWRITE)
  78. IF FERROR() != 0 
  79.         RETURN ERR_DRIVER
  80. ENDIF
  81.  
  82.  
  83. /* write out the control registers */
  84.  
  85. IOCTL_OUT(nHandle, "v=INITIATE ")
  86. IOCTL_OUT(nHandle, "a=" + app + " ")
  87. IOCTL_OUT(nHandle, "t=" + topic + " ")
  88.  
  89.  
  90. /* wait for completion */
  91.  
  92.  
  93. rc = IOCTL_IN(nHandle) 
  94. timeout = SECONDS()
  95.  
  96. DO WHILE ABS(SECONDS() - timeout) <= waittime  .AND. rc == "12" 
  97.          rc = IOCTL_IN(nHandle) 
  98. ENDDO
  99.  
  100.  
  101. /* close device and return result. */
  102.  
  103. FCLOSE(nHandle)
  104. RETURN VAL(rc)
  105.  
  106.  
  107.  
  108. /****************************DDETerminate**********************************
  109. **
  110. ** Terminate a conversation with a Windows program.
  111. **
  112. ** Input :
  113. ** An open DDE channel number
  114. ** A timeout value.
  115. **
  116. ** Output :
  117. ** error_code Success or Failure.
  118. ** 
  119. ** Example:    error_code = DDETerminate(channel, 30)
  120. **
  121. ** This example closes a channel with a Windows program.
  122. ************************************************************************/
  123.  
  124.  
  125. FUNCTION DDETerminate(channel, waittime)
  126. LOCAL nHandle, timeout, rc
  127.  
  128. IF channel = NIL  
  129.    channel = 1 
  130. ENDIF
  131.  
  132.  
  133. IF waittime = NIL
  134.    waittime = DDE_DEFAULT_WAIT
  135. ENDIF
  136.  
  137.  
  138. IF VALTYPE(channel) != "N" .OR. VALTYPE(waittime) != "N"
  139.    RETURN ERR_PARM_TYPE
  140. ENDIF
  141.  
  142.  
  143. /* open the dde device */
  144.  
  145. nHandle = FOPEN("ddedrv", FO_READWRITE)
  146. IF FERROR() != 0 
  147.         RETURN ERR_DRIVER
  148. ENDIF
  149.  
  150.  
  151. /* write out the control registers */
  152.  
  153. IOCTL_OUT(nHandle, "v=TERMINATE ")
  154. IOCTL_OUT(nHandle, "c=" + STR(channel) + " ")
  155.  
  156.  
  157. /* wait for completion */
  158.  
  159. timeout = SECONDS()
  160. rc = IOCTL_IN(nHandle)
  161. DO WHILE ABS(SECONDS() - timeout) <= waittime .AND. rc == "12"
  162.         rc = IOCTL_IN(nHandle)   
  163. ENDDO
  164.  
  165. /* close device and return result. */
  166.  
  167. FCLOSE(nHandle)
  168. RETURN VAL(rc)
  169.  
  170.  
  171. /****************************DDEExecute**********************************
  172. **
  173. ** Perform an Execute through an open channel.
  174. **
  175. ** Input :
  176. ** An open DDE channel number
  177. ** A command string to execute
  178. ** A timeout value.
  179. **
  180. ** Output :
  181. ** error_code Success or Failure.
  182. ** 
  183. ** Example:  error_code = DDEExecute(channel, "[CreateGroup(Clipper)]", 30)
  184. **
  185. ** This example sends the command "[CreateGroup(Clipper)]" to a Windows
  186. ** program.  If the channel was opened with the Windows program manager
  187. ** this would result in a new program group being added.
  188. ************************************************************************/
  189.  
  190.  
  191. FUNCTION DDEExecute(channel, command_str, waittime)
  192. LOCAL nHandle, timeout, rc
  193.  
  194. IF command_str = NIL
  195.     RETURN DDE_PARM_ERROR
  196. ENDIF
  197.  
  198. IF VALTYPE(command_str) != "C"
  199.     RETURN DDE_PARM_TYPE
  200. ENDIF
  201.  
  202. IF channel = NIL  
  203.    channel = 1 
  204. ENDIF
  205.  
  206.  
  207. IF waittime = NIL
  208.    waittime = DDE_DEFAULT_WAIT
  209. ENDIF
  210.  
  211.  
  212. IF VALTYPE(channel) != "N" .OR. VALTYPE(waittime) != "N"
  213.    RETURN ERR_PARM_TYPE
  214. ENDIF
  215.  
  216.  
  217. /* open the dde device */
  218.  
  219. nHandle = FOPEN("ddedrv", FO_READWRITE)
  220. IF FERROR() != 0 
  221.         RETURN ERR_DRIVER
  222. ENDIF
  223.  
  224.  
  225. /* write out the control registers */
  226.  
  227. IOCTL_OUT(nHandle, "v=EXECUTE ")
  228. IOCTL_OUT(nHandle, "c=" + STR(channel) + " ")
  229. IOCTL_OUT(nHandle, "i=" + command_str + " ")
  230.  
  231.  
  232. /* wait for completion */
  233.  
  234. timeout = SECONDS()
  235. rc = IOCTL_IN(nHandle)
  236. DO WHILE ABS(SECONDS() - timeout) <= waittime .AND. rc == "12"
  237.         rc = IOCTL_IN(nHandle)   
  238. ENDDO
  239.  
  240. /* close device and return result. */
  241.  
  242. FCLOSE(nHandle)
  243. RETURN VAL(rc)
  244.  
  245.  
  246.  
  247. /****************************DDERequest**********************************
  248. **
  249. ** Request an item from an open channel.
  250. **
  251. ** Input :
  252. ** An open DDE channel number
  253. ** An item name
  254. ** A buffer to receive the item's value 
  255. ** A timeout value.
  256. **
  257. ** Output :
  258. ** error_code Success or Failure.
  259. ** buffer is filled with item's value
  260. ** 
  261. ** Example:  error_code = DDERequest(channel, "Groups", groups, 30)
  262. **
  263. ** This example reqeusts the value of the item named "Groups" from a 
  264. ** Windows program using the open channel.
  265. ** If the channel was opened with the Windows program manager
  266. ** this request would result with the buffer being filled with
  267. ** a list of all the program group names.
  268. ************************************************************************/
  269.  
  270.  
  271. FUNCTION DDERequest(channel, item, buffer, waittime)
  272. LOCAL nHandle, timeout, rc, byte
  273.  
  274. IF item = NIL  .OR. buffer = NIL
  275.     RETURN DDE_PARM_ERROR
  276. ENDIF
  277.  
  278. IF VALTYPE(item) != "C"
  279.     RETURN DDE_PARM_TYPE
  280. ENDIF
  281.  
  282. IF channel = NIL  
  283.    channel = 1 
  284. ENDIF
  285.  
  286.  
  287. IF waittime = NIL
  288.    waittime = DDE_DEFAULT_WAIT
  289. ENDIF
  290.  
  291.  
  292. IF VALTYPE(channel) != "N" .OR. VALTYPE(waittime) != "N"
  293.    RETURN ERR_PARM_TYPE
  294. ENDIF
  295.  
  296.  
  297. /* open the dde device */
  298.  
  299. nHandle = FOPEN("ddedrv", FO_READWRITE)
  300. IF FERROR() != 0 
  301.         RETURN ERR_DRIVER
  302. ENDIF
  303.  
  304.  
  305. /* write out the control registers */
  306.  
  307. IOCTL_OUT(nHandle, "v=REQUEST ")
  308. IOCTL_OUT(nHandle, "c=" + STR(channel) + " ")
  309. IOCTL_OUT(nHandle, "i=" + item + " ")
  310.  
  311.  
  312. /* wait for completion */
  313.  
  314. timeout = SECONDS()
  315. rc = IOCTL_IN(nHandle)
  316. DO WHILE ABS(SECONDS() - timeout) <= waittime .AND. rc == "12"
  317.         rc = IOCTL_IN(nHandle)   
  318. ENDDO
  319.  
  320. IF rc == "0"
  321.    byte = FREADSTR(nHandle, 1)
  322.    DO WHILE ASC(byte) != 0
  323.        buffer = buffer + byte
  324.        byte = FREADSTR(nHandle, 1)
  325.    ENDDO
  326. ENDIF
  327.  
  328. /* close device and return result. */
  329.  
  330. FCLOSE(nHandle)
  331. RETURN VAL(rc)
  332.  
  333.  
  334. /****************************DDEPoke**********************************
  335. **
  336. ** Poke a value into an item through an open channel.
  337. **
  338. ** Input :
  339. ** An open DDE channel number
  340. ** An item name
  341. ** The item's new value 
  342. ** A timeout value.
  343. **
  344. ** Output :
  345. ** error_code Success or Failure.
  346. ** 
  347. ** Example:  error_code = DDEPoke(channel, "R1C1", cell, 30)
  348. **
  349. ** This example pokes the item "R1C1" with the value cell.  If the channel
  350. ** opened with an Excel spreadsheet, this DDEPoke would update the value
  351. ** of a spreadsheet cell.
  352. ************************************************************************/
  353.  
  354.  
  355. FUNCTION DDEPoke(channel, item, value, waittime)
  356. LOCAL nHandle, timeout, rc, i
  357.  
  358. IF item = NIL  .OR. value = NIL
  359.     RETURN DDE_PARM_ERROR
  360. ENDIF
  361.  
  362. IF VALTYPE(item) != "C"  .AND. VALTYPE(value) != "C"
  363.     RETURN DDE_PARM_TYPE
  364. ENDIF
  365.  
  366. IF channel = NIL  
  367.    channel = 1 
  368. ENDIF
  369.  
  370.  
  371. IF waittime = NIL
  372.    waittime = DDE_DEFAULT_WAIT
  373. ENDIF
  374.  
  375.  
  376. IF VALTYPE(channel) != "N" .OR. VALTYPE(waittime) != "N"
  377.    RETURN ERR_PARM_TYPE
  378. ENDIF
  379.  
  380.  
  381. /* open the dde device */
  382.  
  383. nHandle = FOPEN("ddedrv", FO_READWRITE)
  384. IF FERROR() != 0 
  385.         RETURN ERR_DRIVER
  386. ENDIF
  387.  
  388.  
  389. /* write out the control registers */
  390.  
  391. IOCTL_OUT(nHandle, "v=POKE ")
  392. IOCTL_OUT(nHandle, "c=" + STR(channel) + " ")
  393. IOCTL_OUT(nHandle, "i=" + item + " ")
  394.  
  395.  
  396. /* write out the value as a sequence of bytes */
  397.  
  398. FOR i:= 1 to LEN(value)
  399.    FWRITE(nHandle, SUBSTR(value, i, 1), 1)
  400. NEXT
  401.  
  402. /* wait for completion */
  403.  
  404. timeout = SECONDS()
  405. rc = IOCTL_IN(nHandle)
  406. DO WHILE ABS(SECONDS() - timeout) <= waittime .AND. rc == "12"
  407.         rc = IOCTL_IN(nHandle)   
  408. ENDDO
  409.  
  410. /* close device and return result. */
  411.  
  412. FCLOSE(nHandle)
  413. RETURN VAL(rc)
  414.  
  415.